package easy;

public class Solution_696 {

    public int countBinarySubstrings(String s) {
        int[] array = new int[s.length()];
        array[0] = -1;
        for (int i = 1; i < s.length(); i++) {
            array[i] = -1;
            boolean un_same = valid(s, i, i - 1);
            if (un_same) {
                array[i] = i - 1;
            } else {
                int preIndex = array[i - 1];
                if (preIndex > 0 && s.charAt(preIndex) == s.charAt(preIndex - 1)) {
                    array[i] = preIndex - 1;
                }
            }
        }
        int count = 0;
        for (int i : array) {
            if (i >= 0) {
                count++;
            }
        }
        return count;
    }

    private boolean valid(String s, int i, int j) {
        return (s.charAt(i) == '0' && s.charAt(j) == '1') || (s.charAt(i) == '1' && s.charAt(j) == '0');
    }

    public static void main(String[] args) {
        Solution_696 model = new Solution_696();
        System.out.println(model.countBinarySubstrings("00110011"));
        System.out.println(model.countBinarySubstrings("10101"));
    }
}
